"""
1799250443@qq.com
hflhpvrvhjkxccda

邮箱服务器域名：smtp.qq.com 端口：465或587
"""
import smtplib # 发送邮件
# 构造邮件内容
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

# 发信方邮箱和授权码
from_addr='1799250443@qq.com'
password='nbukekpuuhhsbagb'
# 收信方邮箱
to_addr='1799250443@qq.com'
# 发信服务器
smtp_server='smtp.qq.com'
port=465

# 邮件正文
# 纯文本
# msg=MIMEText('使用python发送邮件测试','plain','utf-8')

# HTML
html_msg="""
<p>你好这是一个测试邮件，请查收：</p>
<a href='https://www.runoob.com/'>菜鸟教程</a>
"""
# msg=MIMEText(html_msg,'html','utf-8')

# 带附件的邮件
msg=MIMEMultipart()
attr=MIMEText(html_msg,'html','utf-8')
msg.attach(attr) # 正文
# 构造附件
att1=MIMEText(open(r'I:\cc.txt','rb').read(),'base64','utf-8')
# 设置邮件附件的HTTP头部信息的。这个头部信息告诉邮件客户端如何处理附件。
att1.add_header('Content-Disposition', 'attachment', filename='test.txt')
msg.attach(att1)

att2=MIMEImage(open(r'I:\Knight.png','rb').read(),'png')
att2.add_header('Content-Disposition', 'attachment', filename='Knight.png')
msg.attach(att2)

# 邮件头信息
msg['From']=Header(from_addr)
msg['To']=Header(to_addr)
subject='python smtp 邮件测试'
msg['Subject']=Header(subject,'utf-8')

try:
    # 建立连接，返回SMTP对象
    smtpobj = smtplib.SMTP_SSL(smtp_server,port)
    # 登录（邮箱和授权码）
    smtpobj.login(from_addr,password)
    # 发送邮件（发件人，收件人，邮件内容）
    smtpobj.sendmail(from_addr,to_addr,msg.as_string())
    print('邮件发送成功')
except Exception as e:
    print('无法发送邮件：',e)
finally:
    smtpobj.quit()



